<?php
    class AjaxController extends Zend_Controller_Action
    {

        public function init()
        {
            $this->_helper->viewRenderer->setNoRender();
            $this->_helper->layout->disableLayout();

            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();

            $this->user_id = false;
            if (!empty($y))
                $this->user_id = $x->read()->rkn_uid;


            $this->user = (array) $y;

            $this->commonModel = new Application_Model_Common;
        }

        function likeAction()
        {

            $message = array();

            $message['success'] = false;
            $message['error'] = true;
            $message['code'] = 203;
            $message['message'] = "Oops!! Some problem occurred";

            if (empty($this->user_id))
            {
                $message['error'] = true;
                $message['success'] = false;
                $message['code'] = 202;
                $message['message'] = "User not logged in";
                exit(json_encode($message));
            }

            $product_id = $this->getRequest()->getParam("product_id");
            $likeArr = $this->commonModel->getAllRecordDetail(RKN_LIKES, "like_user_id = '" . $this->user_id . "' and like_prod_id = '$product_id' ");

            if (count($likeArr))
            {

                $result = $this->commonModel->deleteRecord("like_user_id = '" . $this->user_id . "' and like_prod_id = '$product_id' ", RKN_LIKES);

                $message['success'] = true;
                $message['error'] = false;
                $message['code'] = 200;
                $message['message'] = "Deleted like";
                
            }
            else
            {
                $data = array();
                $data['like_prod_id'] = $product_id;
                $data['like_user_id'] = $this->user_id;
                $result = $this->commonModel->insertRecordData($data, RKN_LIKES);

                if ($result)
                {
                    $message['success'] = true;
                    $message['error'] = false;
                    $message['code'] = 200;
                    $message['message'] = "Successfully liked";
                }
                else
                {
                    $message['success'] = false;
                    $message['error'] = true;
                    $message['code'] = 203;
                    $message['message'] = "Oops!! Some problem occurred";
                }
            }
            echo json_encode($message);
        }
        
        public function connectAction(){
            
            session_start();
            if(isset($_SESSION['connect']))
                $connect = $_SESSION['connect'];
            else
                $connect = 'off';
            
            if($connect == 'off' || empty($connect)){
                $_SESSION['connect'] = 'on';
            }else{
                $_SESSION['connect'] = 'off';
            }
        }

    }